package com.surfilter.geet.code;

import com.surfilter.geet.RamdomUtil;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.io.File;
import java.util.UUID;

//
public class MoveTest {
    private static Logger logger = LoggerFactory.getLogger(MoveTest.class);
    private static Robot robot = null;
    static{
        try {
            robot = new Robot();
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws InterruptedException, Exception {
        DesiredCapabilities dcp = new DesiredCapabilities();
        dcp.setJavascriptEnabled(true);
        dcp.setPlatform(Platform.WINDOWS);
        dcp.setBrowserName("Chrome");
        dcp.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36");
        dcp.setCapability("phantomjs.page.settings.loadImages", true);
        dcp.setCapability("phantomjs.page.settings.resourceTimeout", 3000);
        dcp.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "E:\\software\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
        PhantomJSDriver driver = new PhantomJSDriver(dcp);
        driver.manage().window().setSize(new Dimension(1440, 900));
        driver.get("http://localhost:8080/gee-data/move.html");
        int num = 0;
        boolean target = false;
        while(true){
            onMouseMoveTest(driver);
            num ++;
            if(num >10000)break;
            Thread.sleep(1000);
        }
        driver.close();
        driver.quit();
    }


    public static void onMouseMoveTest(PhantomJSDriver driver) throws InterruptedException{
        String srcObjectSelector = "#srcObject";
        String targetObjSelector = "#targetObj";
        //找到两个滑块位置
        RemoteWebElement srcElement = (RemoteWebElement) driver.findElementByCssSelector(srcObjectSelector);
        RemoteWebElement targetElement = (RemoteWebElement) driver.findElementByCssSelector(targetObjSelector);
        //计算距离
        int length = targetElement.getLocation().getX() - srcElement.getLocation().getX();
        logger.info("滑块之间的距离是：" + length);
        //移动到位置
        driver.getMouse().mouseMove(srcElement.getCoordinates());
        logger.info("src = [" + srcElement.getLocation().getX() + "," + srcElement.getLocation().getY() +"]");
        //鼠标落下
        try {
            driver.getMouse().mouseDown(srcElement.getCoordinates());
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        //鼠标移动
        moveRemoteWebElementWindows(driver, length);

        String uuid = String.valueOf(UUID.randomUUID());
//        saveImage(driver, "C:/opt/rzx/geente/" + uuid + ".jpg");
        //鼠标抬起
        driver.getMouse().mouseUp(srcElement.getCoordinates());

        robot.mouseMove(srcElement.getLocation().getX(), srcElement.getLocation().getY());
    }

    public static void moveRemoteWebElementLinux(PhantomJSDriver driver, int length){
        int px = MouseInfo.getPointerInfo().getLocation().x;
        int py = MouseInfo.getPointerInfo().getLocation().y;
        long start = System.currentTimeMillis();
        long end = start;
        try{
            int sum = 0;
            int max = 15;
            int min = 5;
            int sleep_max = 500;
            int sleep_min = 100;
            while(true){
                int subLength = RamdomUtil.getRandomNum(min, max);
                int x = 0;
                int y = RamdomUtil.getRandomNum(-3, 3);
                if(sum + subLength > length){
                    x = length - sum;
                }else{
                    x = subLength;
                }
                driver.getMouse().mouseMove(null, x, y);
                px += x;
                py += y;
                robot.mouseMove(px, py);
                sum += subLength;
                if(sum >= length)break;
                Thread.sleep(RamdomUtil.getRandomNum(sleep_min, sleep_max));
                if(length - sum < 50){
                    max = 4;
                    min = -1;
                    sleep_min = 10;
                    sleep_max = 40;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        end = System.currentTimeMillis();
        System.out.println("移动鼠标耗时:" + (end - start));
    }
//windows环境下
    public static void moveRemoteWebElementWindows(PhantomJSDriver driver, int length){
        int px = MouseInfo.getPointerInfo().getLocation().x;
        int py = MouseInfo.getPointerInfo().getLocation().y;
        long start = System.currentTimeMillis();
        long end = start;
        try{
            int sum = 0;
            int max = 10;
            int min = 5;
            int sleep_max = 300;
            int sleep_min = 50;
            while(true){
                int subLength = RamdomUtil.getRandomNum(min, max);
                int x = 0;
                int y = RamdomUtil.getRandomNum(-3, 2);
                if(sum + subLength > length){
                    x = length - sum;
                }else{
                    x = subLength;
                }
                driver.getMouse().mouseMove(null, x, y);
                px += x;
                py += y;
                robot.mouseMove(px, py);

                sum += subLength;
                if(sum >= length)break;
                Thread.sleep(RamdomUtil.getRandomNum(sleep_min, sleep_max));
                if(length - sum < 15){
                    max = 4;
                    min = -2;
                    sleep_min = 50;
                    sleep_max = 100;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        end = System.currentTimeMillis();
        System.out.println("移动鼠标耗时:" + (end - start));
    }

    public static void saveImage(PhantomJSDriver driver, String filePath){
        File destFile = new File(filePath);
        if(destFile.exists())destFile.delete();
        File sourceFile = driver.getScreenshotAs(OutputType.FILE);
        sourceFile.renameTo(new File(filePath));
    }


}
